﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Net;
using System.Collections;
using System.Xml;
using System.Linq;
//using System.Xml.Linq;

using CommonUtils;
using TDAL = EzDAL.Auto2;
using TDALUtils = EzDAL.Utils;
using TConst = TERP.Constants;
using TUtils = TERP.Utils;
using TError = TERP.Errors;


namespace TERP.BizITS
{
	public partial class BizWater
	{
		#region // Private Utils:
		#endregion

		#region //Validate

		#region //StockIn
		#region //CheckExistStockInID
		private void CheckExistStockInID(
		   ref ArrayList alParamsCoupleError
		   , string strStockInID
		   , out DataTable dtStockIn
		)
		{
			dtStockIn = TDALUtils.DBUtils.GetTableContents(
				_dbCarSv // db
				, "Water_StockIn" // strTableName
				, "top 1 *" // strColumnList
				, "" // strClauseOrderBy
				, "StockInID", "=", strStockInID // arrobjParamsTriple item
			 );

			if (dtStockIn == null || dtStockIn.Rows.Count == 0)
			{

				throw CMyException.Raise(
					TError.ErrITS.Its_StockInID_Not_Found
					, null
					, alParamsCoupleError.ToArray()
					);
			}
		}
		#endregion
		#endregion


		#endregion

		#region //StockIn

		#region Its_StockIn_Get
		public DataSet Its_StockIn_Get(
		string strGWUserCode
		, string strGWPassword
		, string strTid
		, string strPartnerCode
		, string strPartnerUserCode
		, string strLanguageCode
		, string strStockInIDConditionList
		, string strStockInDateConditionList
		, string strSupplierIDConditionListt
		)
		{

			#region // Temp:

			DataSet mdsFinal = CMyDataSet.NewMyDataSet(strTid);

			int nTidSeq = 0;
			bool bNeedTransaction = false;
			string strFunctionName = "Its_StockIn_Get";
			string strErrorCodeDefault = TError.ErrITS.Its_StockIn_Get;
			ArrayList alParamsCoupleError = new ArrayList(new object[]{

					"strFunctionName", strFunctionName
                    , "strStockIDConditionList", strStockInIDConditionList
					, "strStockInDateConditionList", strStockInDateConditionList
                    ,"strSupplierIDConditionList",strSupplierIDConditionListt
					});

			#endregion

			try
			{

				#region // Init:

				_dbCarSv.AppUserID = strPartnerUserCode;

				if (bNeedTransaction) _dbCarSv.BeginTransaction();

				// Write request log:

				_log.WriteLogAsync(

				_strConfig_GwUserCode // strGwUserCodeForLog

				, _strConfig_GwUserPasswordForLog // strGwPasswordForLog

				, strTid // strAppTid

				, strPartnerCode // strAppServiceCode

				, strPartnerUserCode // strAppUPartCode

				, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") // strAppTDateTime

				, TError.Error.NoError // strAppErrorCode

				, strFunctionName // strAppLogType1

				, "REQUEST" // strAppLogType2

				, "" // strAppLogType3

				, strLanguageCode // strAppLanguageCode

				, "" // strAppRemark

				, TUtils.CUtils.ParamsCouple2ParamsTriple(alParamsCoupleError, 1, "").ToArray() // arrobjParamsTriple

				);

				// Check Valid Tid:

				myUtils_ValidateId(

				ref alParamsCoupleError // alParamsCoupleError

				, strTid // strTid

				, TError.ErrITS.CommonInvalidTid // strErrCodeWhenInvalid

				);

				#endregion

				#region // Build Sql:

				ArrayList alParamsCoupleSql = new ArrayList();

				string strSqlGetData = @"
						SELECT
                                t.*
								, sup.SupplierName
								--, t.Price*t.Quantity AS AMOUNT
								--, t.Price*t.Quantity + t.PriceCuocVo*t.CuocVo -t.AmountPaid AS AMOUNTDEBIT
                        FROM Water_StockIn t with (nolock)
						left join Water_Supplier sup on t.SupplierID = sup.SupplierID
						WHERE (1=1)
                                    zzzzClauseWhereStockInIDConditionList
                                    zzzzClauseWhereStockInDateConditionList
									zzzzClauseWhereSupplierIDConditionList
									;
				";

				string zzzzClauseWhereStockInIDConditionList = SqlUtils.BuildClause("and", "t.StockInID", strStockInIDConditionList, "@p", ref alParamsCoupleSql);
				string zzzzClauseWhereStockInDateConditionList = SqlUtils.BuildClause("and", "t.StockInDate", strStockInDateConditionList, "@p", ref alParamsCoupleSql);
				string zzzzClauseWhereSupplierIDConditionList = SqlUtils.BuildClause("and", "t.SupplierID", strSupplierIDConditionListt, "@p", ref alParamsCoupleSql);


				strSqlGetData = StringUtils.Replace(
				strSqlGetData
				, "zzzzClauseWhereStockInIDConditionList", zzzzClauseWhereStockInIDConditionList
				, "zzzzClauseWhereStockInDateConditionList", zzzzClauseWhereStockInDateConditionList
				, "zzzzClauseWhereSupplierIDConditionList", zzzzClauseWhereSupplierIDConditionList

				);

				#endregion

				#region // Get Data:
				myDebug_SaveSql(
					_bDebugMode // bDebugMode
					, ref alParamsCoupleError // alParamsCoupleError
					, string.Format("strSql_{0}", strFunctionName) // strParamName
					, strSqlGetData // strSql
					);


				DataSet dsGetData = _dbCarSv.ExecQuery(

				strSqlGetData

				, alParamsCoupleSql.ToArray()

				);

				dsGetData.Tables[0].TableName = "Water_StockIn";

				DataUtils.MoveDataTable(ref mdsFinal, ref dsGetData);

				#endregion

				// Return Good:

				TDALUtils.DBUtils.CommitSafety(_dbCarSv);

				mdsFinal.AcceptChanges();

				return mdsFinal;

			}

			catch (Exception exc)
			{

				#region // Catch of try:

				// Rollback:

				TDALUtils.DBUtils.RollbackSafety(_dbCarSv);

				// Return Bad:

				return TUtils.CProcessException.Process(

				ref mdsFinal

				, exc

				, strErrorCodeDefault
				, alParamsCoupleError.ToArray()

				);

				#endregion

			}

			finally
			{

				#region // Finally of try:

				// Rollback and Release resources:

				TDALUtils.DBUtils.RollbackSafety(_dbCarSv);

				TDALUtils.DBUtils.ReleaseAllSemaphore(_dbCarSv_Sys, true);

				TUtils.CProcessException.Process(

				ref mdsFinal // mdsFinal

				, _log // log

				, _strConfig_GwUserCode // strGwUserCodeForLog

				, _strConfig_GwUserPasswordForLog // strGwPasswordForLog

				, strFunctionName // strFunctionName

				, TUtils.CUtils.TidNext(strTid, ref nTidSeq) // strAppTid

				, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") // strAppTDateTime

				, strPartnerCode // strAppServiceCode

				, strPartnerUserCode // strAppUserCode

				, strLanguageCode // strLanguageCode

				, "" // strRemark"

				);

				#endregion

			}

		}

		#endregion

		#region //Its_Water_StockIn_Create
		public DataSet Its_StockIn_Create(
			 string strGWUserCode
			 , string strGWPassword
			 , string strTid
			 , string strPartnerCode
			 , string strPartnerUserCode
			 , string strLanguageCode
			 , string strStockInCode
			 , string strStockInDate
			 , string strNote
			 , string strSupplierID
			 , DataSet dsStockInDetail
				)
		{
			#region // Temp:
			DataSet mdsFinal = CMyDataSet.NewMyDataSet(strTid);
			int nTidSeq = 0;
			bool bNeedTransaction = true;
			string strFunctionName = "Its_Customer_Create";
			string strErrorCodeDefault = TError.ErrITS.Its_Customer_Create;
			ArrayList alParamsCoupleError = new ArrayList(new object[]{
			"strFunctionName", strFunctionName
			, "strStockInCode", strStockInCode
			, "strStockInDate", strStockInDate
			, "strNote", strNote
			, "strSupplierID", strSupplierID
			});
			#endregion
			try
			{
				#region // Init:
				_dbCarSv.AppUserID = strPartnerUserCode;
				if (bNeedTransaction) _dbCarSv.BeginTransaction();
				// Write request log:
				_log.WriteLogAsync(
				_strConfig_GwUserCode // strGwUserCodeForLog
				, _strConfig_GwUserPasswordForLog // strGwPasswordForLog
				, strTid // strAppTid
				, strPartnerCode // strAppServiceCode
				, strPartnerUserCode // strAppUPartCode
				, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") // strAppTDateTime
				, TError.Error.NoError // strAppErrorCode
				, strFunctionName // strAppLogType1
				, "REQUEST" // strAppLogType2
				, "" // strAppLogType3
				, strLanguageCode // strAppLanguageCode
				, "" // strAppRemark
				, TUtils.CUtils.ParamsCouple2ParamsTriple(alParamsCoupleError, 1, "").ToArray() // arrobjParamsTriple
				);
				// Check Valid Tid:
				myUtils_ValidateId(
				ref alParamsCoupleError // alParamsCoupleError
				, strTid // strTid
				, TError.ErrITS.CommonInvalidTid // strErrCodeWhenInvalid
				);
				#endregion

				#region // Check:

				//if (dsBookGuideDetail == null || dsBookGuideDetail.Tables.Count == 0)
				//{
				//    throw CMyException.Raise(
				//   TError.ErrITS.Its_BookingTour_NotFoundDetail
				//   , null
				//   , alParamsCoupleError.ToArray()
				//   );
				//}
				//else
				//{
				//    if (dsBookGuideDetail.Tables["BookGuideDetail"].Rows.Count == 0)
				//    {
				//        throw CMyException.Raise(
				//    TError.ErrITS.Its_BookGuide_NotFoundDetail
				//    , null
				//    , alParamsCoupleError.ToArray()
				//  );
				//    }
				//}
				#endregion

				#region // dt_Book:
				DataTable dt_Book = TDALUtils.DBUtils.GetSchema(_dbCarSv, "Water_StockIn").Tables[0];
				dt_Book.Rows.Add(dt_Book.NewRow());
				dt_Book.Rows[0]["StockInCode"] = strStockInCode;
				if (!StringUtils.IsEmpty(strStockInDate))
				{
					dt_Book.Rows[0]["StockInDate"] = strStockInDate;
				}

				if (!StringUtils.IsEmpty(strNote))
				{
					dt_Book.Rows[0]["Note"] = strNote;
				}

				
				if (!StringUtils.IsEmpty(strSupplierID))
				{
					dt_Book.Rows[0]["SupplierID"] = strSupplierID;
				}			

				//Save
				_dbCarSv.SaveData("Water_StockIn", dt_Book);

				//Get ID
				string strSqlGetTourID = @"select @@Identity StockInID;";
				Int32 iID = Convert.ToInt32(_dbCarSv.ExecQuery(strSqlGetTourID).Tables[0].Rows[0][0]);
				dt_Book.Rows[0]["StockInID"] = iID;
				#endregion

				#region //Process
				ProcessSaveStockInDetail(dsStockInDetail, iID);
				//double dGetAmount = Convert.ToDouble(strAmountPaid) - Convert.ToDouble(strPrice) * Convert.ToDouble(strQuantity);
				//ProcessUpdateCustomerDebitMany(strSupplierID, dGetAmount.ToString(), strNoVo, strCuocVo);
				#endregion

				#region // Return data

				DataSet dsGetData = new DataSet();
				dsGetData.Tables.Add(dt_Book.Copy());
				dsGetData.Tables[0].TableName = "Water_StockIn";

				DataUtils.MoveDataTable(ref mdsFinal, ref dsGetData);

				#endregion

				// Return Good:
				TDALUtils.DBUtils.CommitSafety(_dbCarSv);
				mdsFinal.AcceptChanges();
				return mdsFinal;
			}
			catch (Exception exc)
			{
				#region // Catch of try:
				// Rollback:
				TDALUtils.DBUtils.RollbackSafety(_dbCarSv);
				// Return Bad:
				return TUtils.CProcessException.Process(
				ref mdsFinal
				, exc
				, strErrorCodeDefault
				, alParamsCoupleError.ToArray()
				);
				#endregion
			}
			finally
			{
				#region // Finally of try:
				// Rollback and Release resources:
				TDALUtils.DBUtils.RollbackSafety(_dbCarSv);
				TDALUtils.DBUtils.ReleaseAllSemaphore(_dbCarSv_Sys, true);
				TUtils.CProcessException.Process(
				ref mdsFinal // mdsFinal
				, _log // log
				, _strConfig_GwUserCode // strGwUserCodeForLog
				, _strConfig_GwUserPasswordForLog // strGwPasswordForLog
				, strFunctionName // strFunctionName
				, TUtils.CUtils.TidNext(strTid, ref nTidSeq) // strAppTid
				, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") // strAppTDateTime
				, strPartnerCode // strAppServiceCode
				, strPartnerUserCode // strAppUserCode
				, strLanguageCode // strLanguageCode
				, " // strRemark"
				);
				#endregion
			}
		}

		#endregion

		#region //Its_Water_StockIn_Update
		public DataSet Its_StockIn_Update(
			 string strGWUserCode
			 , string strGWPassword
			 , string strTid
			 , string strPartnerCode
			 , string strPartnerUserCode
			 , string strLanguageCode
			 , string strStockInIn
			 , string strStockInCode
			 , string strStockInDate
			 , string strNote
			 , string strSupplierID
			 , DataSet dsStockInDetail
				)
		{
			#region // Temp:
			DataSet mdsFinal = CMyDataSet.NewMyDataSet(strTid);
			int nTidSeq = 0;
			bool bNeedTransaction = true;
			string strFunctionName = "Its_StockIn_Update";
			string strErrorCodeDefault = TError.ErrITS.Its_StockIn_Update;
			ArrayList alParamsCoupleError = new ArrayList(new object[]{
			"strFunctionName", strFunctionName
			, "strStockInIn", strStockInIn
			, "strStockInCode", strStockInCode
			, "strStockInDate", strStockInDate
			, "strNote", strNote
			, "strSupplierID", strSupplierID
			});
			#endregion
			try
			{
				#region // Init:
				_dbCarSv.AppUserID = strPartnerUserCode;
				if (bNeedTransaction) _dbCarSv.BeginTransaction();
				// Write request log:
				_log.WriteLogAsync(
				_strConfig_GwUserCode // strGwUserCodeForLog
				, _strConfig_GwUserPasswordForLog // strGwPasswordForLog
				, strTid // strAppTid
				, strPartnerCode // strAppServiceCode
				, strPartnerUserCode // strAppUPartCode
				, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") // strAppTDateTime
				, TError.Error.NoError // strAppErrorCode
				, strFunctionName // strAppLogType1
				, "REQUEST" // strAppLogType2
				, "" // strAppLogType3
				, strLanguageCode // strAppLanguageCode
				, "" // strAppRemark
				, TUtils.CUtils.ParamsCouple2ParamsTriple(alParamsCoupleError, 1, "").ToArray() // arrobjParamsTriple
				);
				// Check Valid Tid:
				myUtils_ValidateId(
				ref alParamsCoupleError // alParamsCoupleError
				, strTid // strTid
				, TError.ErrITS.CommonInvalidTid // strErrCodeWhenInvalid
				);
				#endregion

				#region // Check:

				if (dsStockInDetail == null || dsStockInDetail.Tables.Count == 0)
				{
					throw CMyException.Raise(
				   TError.ErrITS.Its_StockInID_TableNotFound
				   , null
				   , alParamsCoupleError.ToArray()
				   );
				}

				DataTable dt_Book = null;
				CheckExistStockInID(ref alParamsCoupleError
								, strStockInIn
								, out dt_Book
								);
				//else
				//{
				//    if (dsBookGuideDetail.Tables["BookGuideDetail"].Rows.Count == 0)
				//    {
				//        throw CMyException.Raise(
				//    TError.ErrITS.Its_BookGuide_NotFoundDetail
				//    , null
				//    , alParamsCoupleError.ToArray()
				//  );
				//    }
				//}
				#endregion

				#region // dt_Book:
				ArrayList alColumnEffective = new ArrayList();
				alColumnEffective.Clear();

				dt_Book.Rows[0]["StockInCode"] = strStockInCode; alColumnEffective.Add("StockInCode");
				if (!StringUtils.IsEmpty(strStockInDate))
				{
					dt_Book.Rows[0]["StockInDate"] = strStockInDate; alColumnEffective.Add("StockInDate");
				}

				if (!StringUtils.IsEmpty(strNote))
				{
					dt_Book.Rows[0]["Note"] = strNote; alColumnEffective.Add("Note");
				}


				if (!StringUtils.IsEmpty(strSupplierID))
				{
					dt_Book.Rows[0]["SupplierID"] = strSupplierID; alColumnEffective.Add("SupplierID");
				}

				//Save
				_dbCarSv.SaveData("Water_StockIn", dt_Book);

				int iID = Convert.ToInt32(strStockInIn);
				#endregion

				#region //Process
				//delete
				ProcessDeleteStockInDetail(ref alParamsCoupleError, iID);

				//save
				ProcessSaveStockInDetail(dsStockInDetail, iID);
				//double dGetAmount = Convert.ToDouble(strAmountPaid) - Convert.ToDouble(strPrice) * Convert.ToDouble(strQuantity);
				//ProcessUpdateCustomerDebitMany(strSupplierID, dGetAmount.ToString(), strNoVo, strCuocVo);
				#endregion

				#region // Return data

				DataSet dsGetData = new DataSet();
				dsGetData.Tables.Add(dt_Book.Copy());
				dsGetData.Tables[0].TableName = "Water_StockIn";

				DataUtils.MoveDataTable(ref mdsFinal, ref dsGetData);

				#endregion

				// Return Good:
				TDALUtils.DBUtils.CommitSafety(_dbCarSv);
				mdsFinal.AcceptChanges();
				return mdsFinal;
			}
			catch (Exception exc)
			{
				#region // Catch of try:
				// Rollback:
				TDALUtils.DBUtils.RollbackSafety(_dbCarSv);
				// Return Bad:
				return TUtils.CProcessException.Process(
				ref mdsFinal
				, exc
				, strErrorCodeDefault
				, alParamsCoupleError.ToArray()
				);
				#endregion
			}
			finally
			{
				#region // Finally of try:
				// Rollback and Release resources:
				TDALUtils.DBUtils.RollbackSafety(_dbCarSv);
				TDALUtils.DBUtils.ReleaseAllSemaphore(_dbCarSv_Sys, true);
				TUtils.CProcessException.Process(
				ref mdsFinal // mdsFinal
				, _log // log
				, _strConfig_GwUserCode // strGwUserCodeForLog
				, _strConfig_GwUserPasswordForLog // strGwPasswordForLog
				, strFunctionName // strFunctionName
				, TUtils.CUtils.TidNext(strTid, ref nTidSeq) // strAppTid
				, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") // strAppTDateTime
				, strPartnerCode // strAppServiceCode
				, strPartnerUserCode // strAppUserCode
				, strLanguageCode // strLanguageCode
				, " // strRemark"
				);
				#endregion
			}
		}

		#endregion

		#region // ProcessSaveStockInDetail
		private void ProcessSaveStockInDetail(DataSet dsStockInDetail, Int32 iROWID)
		{
			#region //Details
			DataTable dtServiceItem = dsStockInDetail.Tables["Water_StockInDetail"];
			if (dtServiceItem != null && dtServiceItem.Rows.Count > 0)
			{
				DataTable dt_ServiceItem = TDALUtils.DBUtils.GetSchema(_dbCarSv, "Water_StockInDetail").Tables[0];
				foreach (DataRow row in dtServiceItem.Rows)
				{
					DataRow newRow = dt_ServiceItem.NewRow();
					newRow["StockInID"] = row["StockInID"];
					if (!StringUtils.IsEmpty(row["MaHang"].ToString()))
					{
						newRow["MaHang"] = row["MaHang"];
					}
					if (!StringUtils.IsEmpty(row["Quantity"].ToString()))
					{
						newRow["Quantity"] = row["Quantity"];
					}
					if (!StringUtils.IsEmpty(row["Cost"].ToString()))
					{
						newRow["Cost"] = row["Cost"];
					}
					if (!StringUtils.IsEmpty(row["Note"].ToString()))
					{
						newRow["Note"] = row["Note"];
					}
					dt_ServiceItem.Rows.Add(newRow);
				}
				if (dt_ServiceItem != null && dt_ServiceItem.Rows.Count > 0)
				{
					_dbCarSv.SaveData("Water_StockInDetail", dt_ServiceItem);
				}
			}
			#endregion //
			
		}
		#endregion

		#region //ProcessDeleteStockInDetail
		private void ProcessDeleteStockInDetail(
			ref ArrayList alParamsCoupleError
			, int iStockInDetail
		 )
		{
			String strFunctionName = "ProcessDeleteStockInDetail";
			string strSqlDelete = @"
						delete 
						from Water_StockInDrtail
						where
							StockInDetailÍ = @iStockInDetail
						;
					";

			// Save:
			myDebug_SaveSql(
				_bDebugMode // bDebugMode
				, ref alParamsCoupleError // alParamsCoupleError
				, string.Format("strSql_{0}", strFunctionName) // strParamName
				, strSqlDelete // strSql
				);
			_dbCarSv.ExecQuery(
				strSqlDelete
				, "@iStockInDetail", iStockInDetail
				);
		}
		#endregion

		#region // Its_StockIn_Delete
		public DataSet Its_StockIn_Delete(
			string strGwUserCode
			, string strGwPassword
			, string strTid
			, string strPartnerCode
			, string strPartnerUserCode
			, string strLanguageCode
			, string strStockInID
		)
		{
			#region // temp
			DataSet mdsFinal = CMyDataSet.NewMyDataSet(strTid);
			int nTidSeq = 0;
			bool bNeedTransaction = true;
			string strFunctionName = "Its_StockIn_Delete";
			string strErrorCodeDefault = TError.ErrITS.Its_StockIn_Delete;
			ArrayList alParamsCoupleError = new ArrayList(new object[]{
					"strFunctionName", strFunctionName
                   ,"strStockInID",strStockInID 
					});
			#endregion

			try
			{
				#region // Init:
				_dbCarSv.AppUserID = strPartnerUserCode;
				if (bNeedTransaction) _dbCarSv.BeginTransaction();

				// Write request log:
				_log.WriteLogAsync(
					_strConfig_GwUserCode // strGwUserCodeForLog
					, _strConfig_GwUserPasswordForLog // strGwPasswordForLog
					, strTid // strAppTid
					, strPartnerCode // strAppServiceCode
					, strPartnerUserCode // strAppUserCode
					, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") // strAppTDateTime
					, TError.Error.NoError // strAppErrorCode
					, strFunctionName // strAppLogType1
					, "REQUEST" // strAppLogType2
					, "" // strAppLogType3
					, strLanguageCode // strAppLanguageCode
					, "" // strAppRemark
					, TUtils.CUtils.ParamsCouple2ParamsTriple(alParamsCoupleError, 1, "").ToArray() // arrobjParamsTriple
					);

				// Check Valid Tid:
				myUtils_ValidateId(
					ref alParamsCoupleError // alParamsCoupleError
					, strTid // strTid
					, TError.ErrHTC.CommonInvalidTid // strErrCodeWhenInvalid
					);
				#endregion

				#region //Customer
				#region //Check
				DataTable dt_Book = null;
				this.CheckExistStockInID
				(
					 ref alParamsCoupleError
					, strStockInID
					, out dt_Book
				 );

				#endregion

				#region // Save data
				string strSqlDelete = @"
						delete 
						from Water_StockIn
						where
							StockInID = @StockInID
						;
					";

				// Save:
				myDebug_SaveSql(
					_bDebugMode // bDebugMode
					, ref alParamsCoupleError // alParamsCoupleError
					, string.Format("strSql_{0}", strFunctionName) // strParamName
					, strSqlDelete // strSql
					);
				_dbCarSv.ExecQuery(
					strSqlDelete
					, "@StockInID", strStockInID
					);



				#endregion
				#endregion

				// Return Good:
				TDALUtils.DBUtils.CommitSafety(_dbCarSv);
				mdsFinal.AcceptChanges();
				return mdsFinal;
			}
			catch (Exception exc)
			{
				#region // Catch of try:
				// Rollback:
				TDALUtils.DBUtils.RollbackSafety(_dbCarSv);

				// Return Bad:
				return TUtils.CProcessException.Process(
					ref mdsFinal
					, exc
					, strErrorCodeDefault
					, alParamsCoupleError.ToArray()
					);
				#endregion
			}
			finally
			{
				#region // Finally of try:
				// Rollback and Release resources:
				TDALUtils.DBUtils.RollbackSafety(_dbCarSv);
				TDALUtils.DBUtils.ReleaseAllSemaphore(_dbCarSv_Sys, true);

				TUtils.CProcessException.Process(
					ref mdsFinal // mdsFinal
					, _log // log
					, _strConfig_GwUserCode // strGwUserCodeForLog
					, _strConfig_GwUserPasswordForLog // strGwPasswordForLog
					, strFunctionName // strFunctionName
					, TUtils.CUtils.TidNext(strTid, ref nTidSeq) // strAppTid
					, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") // strAppTDateTime
					, strPartnerCode // strAppServiceCode
					, strPartnerUserCode // strAppUserCode
					, strLanguageCode // strLanguageCode
					, "" // strRemark
					);
				#endregion
			}
		}
		#endregion

		#endregion
	}
}